package ttOrder.controller;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import ttOrder.service.IMycartService;
import ttOrder.service.ITTUserService;
import ttOrder.vo.TTUserVO;
@Controller
public class loginController {
	@Autowired
	public IMycartService mycart;

	@Autowired  
	ITTUserService ttUserService; 
	 
	//普通用户或服务员登录
	@RequestMapping(value ="/index",method = RequestMethod.GET)
	public String toIndex(){
		return "Index/index";
	}  
	
	  
		
	//注册方法  
    @RequestMapping("/addregister")  
    public String register(HttpServletRequest request){  
        String username = request.getParameter("username");  
        String password = request.getParameter("password");  
        String password2 = request.getParameter("password2"); 
 
        if (password.equals(password2)){  
            TTUserVO ttUserVO = new TTUserVO();  
            ttUserVO.setUserName(username);  
            ttUserVO.setPassword(password);  
            ttUserService.register(ttUserVO);  
            return "index/login";  
        }else {  
            return "index/login";  
        }  
    }    
	
	
	//登录页面  
	    
	    @RequestMapping(value ="/login",method = RequestMethod.GET)
		public String toLogin(){
			return "index/login";
		}  
//	 //登录方法  
//    @RequestMapping(value="/addlogin",method = RequestMethod.POST)  
//    public String login(TTUserVO vo,Model model,HttpSession session){  
//        if(vo.getUserName() != null && vo.getPassword() != null){
//        	TTUserVO ttUserVO = ttUserService.findByUsernameAndPassword(vo.getUserName());
//        	if(ttUserVO != null){
//        		if(vo.getPassword() != null && vo.getPassword()!=""){
//        			
//        			if(vo.getPassword().equals(ttUserVO.getPassword())){
//						//登录成功	        				
//        				if(ttUserVO.getUserRole().equals("服务员")){ 
//        					session.setAttribute("ttUserVO", ttUserVO);
//        					return "redirect:/admin";
//        				}else{
//        					session.setAttribute("ttUserVO", ttUserVO);
//        					return "redirect:/index";
//        				}
//        				
//        			}else{        				
//        			/*	redirectAttribute.addFlashAttribute("messages","密码错误");*/
//        			}        		
//        		}else{
////        			redirectAttribute.addFlashAttribute("messages","密码不能为空");
//        		}
//        	}else{
////        		redirectAttribute.addFlashAttribute("messages","用户名不存在");
//        	}
//        }else{
////        	redirectAttribute.addFlashAttribute("messages","请输入用户名和密码");
//        }
//       
//      
//       
//        return "redirect:/login";
//        
//    }  
	    
	    
		 //登录方法  
	    @RequestMapping(value="/login",method = RequestMethod.POST)
	    @ResponseBody
	    public boolean login(@RequestBody TTUserVO vo,Model model,HttpSession session){  
	    	 boolean result = false;
	        if(vo.getUserName() != null && vo.getPassword() != null){
	        	TTUserVO ttUserVO = ttUserService.findByUsernameAndPassword(vo.getUserName());
	        	if(ttUserVO != null){
	        		if(vo.getPassword() != null && vo.getPassword()!=""){
	        			
	        			if(vo.getPassword().equals(ttUserVO.getPassword())){
							//登录成功	        				
	        				 
	        					session.setAttribute("ttUserVO", ttUserVO);
	        				
	        					result = true;
	        				
	        				
	        			}else{        				
	        			/*	redirectAttribute.addFlashAttribute("messages","密码错误");*/
	        			}        		
	        		}else{
//	        			redirectAttribute.addFlashAttribute("messages","密码不能为空");
	        		}
	        	}else{
//	        		redirectAttribute.addFlashAttribute("messages","用户名不存在");
	        	}
	        }else{
//	        	redirectAttribute.addFlashAttribute("messages","请输入用户名和密码");
	        }
	       
	      
	       
	        return result;
	        
	    }  
	    //退出
		@RequestMapping(value = "/logout")
		public String logout(HttpSession session){
			session.removeAttribute("ttUserVO");
			mycart.clear();	
			return "redirect:/login";
			
		}
}
